﻿package cn.poco.pepsi.adapter;

import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import cn.poco.pepsi.R;
import cn.poco.pepsi.TabTest;
import cn.poco.pepsi.activity.detail.ResDetailActivity;
import cn.poco.pepsi.activity.guest.FoodGuestListActivity;
import cn.poco.pepsi.activity.member.MemberActivity;
import cn.poco.pepsi.bean.Guest;
import cn.poco.pepsi.service.impl.OtherImpl;
import cn.poco.pepsi.util.AsyncImageLoader.ImageCallback;
import cn.poco.pepsi.util.AsyncLoadImageService;
import cn.poco.pepsi.util.Cons;
import cn.poco.pepsi.util.DateUtil;
import cn.poco.pepsi.util.Screen;

/**
 * 同城食客列表适配器
 * 
 * @author tanglx
 * 
 */
public class FoodGuestListAdapter extends BaseAdapter {
	private List<cn.poco.pepsi.bean.Guest> list;
	private ListView listView;
	private Context context;
	private AsyncLoadImageService asyncImgLoader;
	private int flag;
	private OtherImpl ImageVoteService;
	private FoodGuestListAdapter adapter;
	private Guest guest;

	public FoodGuestListAdapter(List<Guest> list, Context context, ListView listView) {
		this.context = context;
		this.listView = listView;
		this.list = list;
		this.asyncImgLoader = new AsyncLoadImageService(context);
		this.listView.setDivider(null);
		this.listView.setDividerHeight(0);
		String contextString = this.context.toString();
		if (contextString.contains(MemberActivity.TAG)) {
			flag = 0;
		} else if (contextString.contains(FoodGuestListActivity.TAG)) {
			flag = 1;
		} else {
			flag = 2;
		}

		ImageVoteService = new OtherImpl();
	}

	public void setAdapter(FoodGuestListAdapter adapter) {
		this.adapter = adapter;
	}

	@Override
	public int getCount() {
		if (list != null) {
			return list.size();
		} else {
			return 0;
		}
	}

	@Override
	public Object getItem(int position) {
		if (list != null) {
			return list.get(position);
		} else {
			return position;
		}
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final CustomLayout clayout;
		Guest guest = list.get(position);
		if (convertView == null) {
			clayout = new CustomLayout();
			convertView = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.guest_list_item, null);
			clayout.userImage = (ImageView) convertView.findViewById(R.id.user_image);
			clayout.userName = (TextView) convertView.findViewById(R.id.user_name);
			clayout.time = (TextView) convertView.findViewById(R.id.guest_time);
			clayout.resName = (TextView) convertView.findViewById(R.id.res_name);
			clayout.gradeImage = (ImageView) convertView.findViewById(R.id.grade_image);
			clayout.gradCommImage = (ImageView) convertView.findViewById(R.id.grade_comm_image);
			clayout.gradeContent = (TextView) convertView.findViewById(R.id.grade_content);
			clayout.produce = (ImageView) convertView.findViewById(R.id.produce);
			clayout.service = (ImageView) convertView.findViewById(R.id.service);
			clayout.average = (TextView) convertView.findViewById(R.id.average);
			clayout.voteCount = (TextView) convertView.findViewById(R.id.vote_cnt);
			convertView.setTag(clayout);
		} else {
			clayout = (CustomLayout) convertView.getTag();
		}

		// clayout.userImage.setTag(position);
		// clayout.userName.setTag(position);
		// clayout.resName.setTag(position);
		// clayout.gradCommImage.setTag(position);

		// 人名
		clayout.userName.setText(guest.getUseName());
		clayout.resName.setText(guest.getResTitle());
		try {
			String strShowDate = DateUtil.getDate(guest.getAddTime());
			clayout.time.setText(strShowDate);
		} catch (Exception e) {
			e.printStackTrace();
		}

		clayout.gradeContent.setText(guest.getGradeContent());
		clayout.average.setText("人均：" + guest.getAverage());
		clayout.voteCount.setText("赞："
				+ (guest.getVoteCnt() == null || guest.getVoteCnt().equals("") ? 0 : guest.getVoteCnt()));

		if (guest.getService() != null && !guest.getService().equals("")) {
			switch (Integer.parseInt(guest.getService())) {
			case Cons.ONE_STAR:
				clayout.service.setImageResource(R.drawable.x1);
				break;
			case Cons.TWO_STAR:
				clayout.service.setImageResource(R.drawable.x2);
				break;
			case Cons.THREE_STAR:
				clayout.service.setImageResource(R.drawable.x3);
				break;
			case Cons.FOUR_STAR:
				clayout.service.setImageResource(R.drawable.x4);
				break;
			case Cons.FIVE_STAR:
				clayout.service.setImageResource(R.drawable.x5);
				break;
			default:
				clayout.service.setImageResource(R.drawable.x0);
				break;
			}
		} else {
			clayout.service.setImageResource(R.drawable.x0);
		}

		if (guest.getProduce() != null && !guest.getProduce().equals("")) {

			switch (Integer.parseInt(guest.getProduce())) {
			case Cons.ONE_STAR:
				clayout.produce.setImageResource(R.drawable.x1);
				break;
			case Cons.TWO_STAR:
				clayout.produce.setImageResource(R.drawable.x2);
				break;
			case Cons.THREE_STAR:
				clayout.produce.setImageResource(R.drawable.x3);
				break;
			case Cons.FOUR_STAR:
				clayout.produce.setImageResource(R.drawable.x4);
				break;
			case Cons.FIVE_STAR:
				clayout.produce.setImageResource(R.drawable.x5);
				break;
			default:
				clayout.produce.setImageResource(R.drawable.x0);
				break;
			}
		} else {
			clayout.produce.setImageResource(R.drawable.x0);
		}
		final String myUserIcon = guest.getUseIcon();
		final String myUserIconTag = myUserIcon + ":" + position;
		if (myUserIcon != null && !myUserIcon.equals("")) {
			clayout.userImage.setTag(myUserIconTag);
			Bitmap drawUserIcon = asyncImgLoader.loadBitmap(myUserIcon, new ImageCallback() {
				@Override
				public void imageLoaded(Bitmap imageBitmap, String imageUrl) {
					ImageView imageViewByTag = (ImageView) listView.findViewWithTag(myUserIconTag);
					if (imageViewByTag != null) {
						imageViewByTag.setImageBitmap(imageBitmap);
					}
				}
			}, true, false, 0);
			if (drawUserIcon == null) {
				clayout.userImage.setImageResource(R.drawable.load_header_imge);
			} else {
				clayout.userImage.setImageBitmap(drawUserIcon);
			}
		} else {
			clayout.userImage.setImageResource(R.drawable.icon);
			clayout.userImage.setTag(position);
		}

		final String myGradeImage = guest.getImageUrl();
		final String myGradeImageTag = myGradeImage + ":" + position;
		if (myGradeImage != null && !myGradeImage.equals("")) {
			clayout.gradeImage.setVisibility(View.VISIBLE);
			clayout.gradCommImage.setVisibility(View.VISIBLE);
			clayout.gradeImage.setTag(myGradeImageTag);
			Bitmap drawCommImage = asyncImgLoader.loadBitmap(myGradeImage, new ImageCallback() {
				@Override
				public void imageLoaded(Bitmap imageBitmap, String imageUrl) {
					ImageView imageViewByTag = (ImageView) listView.findViewWithTag(myGradeImageTag);
					if (imageViewByTag != null) {
						imageViewByTag.setImageBitmap(imageBitmap);
					}
				}
			}, false, true, Screen.dip2px(context, 260));
			if (drawCommImage == null) {
				clayout.gradeImage.setImageResource(R.drawable.load_guest);
			} else {
				clayout.gradeImage.setImageBitmap(drawCommImage);
			}
		} else {
			clayout.gradeImage.setTag(position);
			clayout.gradeImage.setVisibility(View.GONE);
			clayout.gradCommImage.setVisibility(View.GONE);
		}

		OnClickListener clickListener = new OnClickListener() {
			@Override
			public void onClick(View v) {
				final Guest guest = list.get(position);
				if(guest.getUseId()!=null&&!guest.getUseId().equals("0")){
					switch (v.getId()) {
					case R.id.user_name:
						if (flag != 0) {
							Intent intent = new Intent(context, MemberActivity.class);
							intent.putExtra(Cons.MemeOnType, 1);
							intent.putExtra(Cons.MemeberName, list.get(position).getUseName());
							intent.putExtra(Cons.MemeberUid, Integer.parseInt(list.get(position).getUseId()));
							context.startActivity(intent);
						}
						break;
					case R.id.user_image:
						if (flag != 0) {
							Intent intent = new Intent(context, MemberActivity.class);
							intent.putExtra(Cons.MemeOnType, 1);
							intent.putExtra(Cons.MemeberName, list.get(position).getUseName());
							intent.putExtra(Cons.MemeberUid, Integer.parseInt(list.get(position).getUseId()));
							context.startActivity(intent);
						}
						break;
					case R.id.res_name:
						if (flag != 2) {
							Intent intent = new Intent(context, ResDetailActivity.class);
							intent.putExtra(Cons.RES_CODE, list.get(position).getResId());
							context.startActivity(intent);
						}
						break;
					case R.id.grade_comm_image:
						SharedPreferences sf = context.getSharedPreferences(Cons.SFPocoUser, 0);
						final String pocoid = sf.getString(Cons.SFPocoId, "0");
						if (pocoid != null && !pocoid.equals("0")) {
							new Thread(new Runnable() {
								public void run() {
									try {
										boolean b = ImageVoteService.votImage(Integer.parseInt(pocoid), guest.getImageId(),
												61, Integer.parseInt(guest.getGradeId()), Integer.parseInt(guest.getUseId()));
										if (b) {
											Integer voteCnt = Integer.parseInt(guest.getVoteCnt());
											System.out.println(voteCnt + ":baseVoteCnt");
											guest.setVoteCnt(String.valueOf(voteCnt + 1));
											((Activity) context).runOnUiThread(new Runnable() {
												public void run() {
													System.out.println("runOnUiThread");
													Toast.makeText(context, "对图片赞成功！", Toast.LENGTH_SHORT).show();
													adapter.notifyDataSetChanged();
												}
											});
										}
									} catch (Exception e) {
										e.printStackTrace();
									}
								}
							}).start();
						} else {
							// 提醒登录
							AlertDialog.Builder builder;
							builder = new Builder(context);
							builder.setMessage("请登录后操作");
							builder.setTitle("提示");
							builder.setPositiveButton("确认", new android.content.DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog, int which) {
									//LoginActivity
									Intent in = new Intent(context, TabTest.class);
									((Activity) context).startActivity(in);
								}
							});
							builder.setNegativeButton("取消", new android.content.DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog, int which) {
									dialog.dismiss();
								}
							});
							builder.create().show();
						}
						break;
					case R.id.vote_cnt:
						break;
					default:
						break;
					}
				}else{
					Toast.makeText(context, "游客食评，不能查看其信息！", Toast.LENGTH_SHORT).show();
				}
				
			}
		};

		clayout.userImage.setOnClickListener(clickListener);

		clayout.userName.setOnClickListener(clickListener);

		clayout.resName.setOnClickListener(clickListener);

		clayout.gradCommImage.setOnClickListener(clickListener);

		return convertView;
	}

	class CustomLayout {
		ImageView userImage, gradeImage, gradCommImage, produce, service;
		TextView userName, resName, time, average, voteCount, gradeContent;
	}

}
